package git.soulbgm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import git.soulbgm.common.enums.Status;
import git.soulbgm.common.pojo.PageInfo;
import git.soulbgm.mapper.UserMapper;
import git.soulbgm.pojo.dto.UserDto;
import git.soulbgm.pojo.entity.User;
import git.soulbgm.service.RoleService;
import git.soulbgm.service.UserService;
import git.soulbgm.utils.ModelBuilder;
import git.soulbgm.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 用户相关业务逻辑
 *
 * @author SoulBGM
 * @date 2020-09-10 17:14:11
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleService roleService;

    private Example getUserExample(UserDto user) {
        Example userExample = new Example(User.class);
        Example.Criteria criteria = userExample.and();
        criteria.andEqualTo("delFlag", Status.EXIST.code);
        if (StringUtil.isNotEmpty(user.getNickname())) {
            criteria.andLike("nickname", "%" + user.getNickname() + "%");
        }
        if (StringUtil.isNotEmpty(user.getUsername())) {
            criteria.andLike("username", "%" + user.getUsername() + "%");
        }
        if (user.getStatus() != null) {
            criteria.andEqualTo("status", user.getStatus());
        }
        if (user.getCreateStartTime() != null && user.getCreateEndTime() != null) {
            criteria.andBetween("createTime", user.getCreateStartTime(), user.getCreateEndTime());
        }
        return userExample;
    }

    @Override
    public PageInfo find(Integer pageNum, Integer pageSize, UserDto user) {
        PageHelper.startPage(pageNum, pageSize);
        Example userExample = getUserExample(user);
        List<User> list = userMapper.selectByExample(userExample);
        return PageInfo.getPageInfo((Page) list);
    }

    @Override
    public User findById(Long id) {
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public User add(User user, List<Long> roleIdList) {
        User userQueryParam = new User();
        userQueryParam.setUsername(user.getUsername());
        int count = userMapper.selectCount(userQueryParam);
        if (count > 0) {
            throw new RuntimeException("用户名已存在");
        }
        ModelBuilder.builder(user);
        int result = userMapper.insertSelective(user);
        if (result > 0) {
            if (roleIdList != null && !roleIdList.isEmpty()) {
                roleService.addUserRole(user.getId(), roleIdList);
            }
            return user;
        } else {
            return null;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean update(User user, List<Long> roleIdList) {
        int result = userMapper.updateByPrimaryKeySelective(user);
        if (result > 0) {
            if (roleIdList != null && !roleIdList.isEmpty()) {
                roleService.deleteUserRole(user.getId());
                roleService.addUserRole(user.getId(), roleIdList);
            }
            return true;
        }
        return false;
    }

    @Override
    public Boolean delete(List idList) {
        Example userExample = new Example(User.class);
        userExample.and().andIn("id", idList);
        int result = userMapper.deleteByExample(userExample);
        return result > 0;
    }

}
